﻿using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using NhJump.Persistence.Context.WcfBinding;
using NhJump.Persistence.Wcf.Autofac.Model;
using NhJump.Persistence.Wcf.Autofac.Repositories;

namespace NhJump.Persistence.Wcf.Autofac.Services
{
    [ServiceContract]
    public interface IPersistenceService
    {
        [OperationContract]
        Customer[] GetCustomers();

        [OperationContract]
        Customer CreateCustomer(string name);

        [OperationContract]
        void DeleteCustomerByName(string name);

        [OperationContract]
        IList<Customer> GetCustomersByName(string name);
    }

    [UnitOfWork]
    public class PersistenceService : IPersistenceService
    {
        private readonly ICustomerRepository _customerRepository;

        public PersistenceService(ICustomerRepository customerRepository)
        {
            _customerRepository = customerRepository;
        }

        public Customer[] GetCustomers()
        {
            return _customerRepository.GetAll().ToArray();
        }

        public Customer CreateCustomer(string name)
        {
            var customer = new Customer() {Name = name};

            _customerRepository.SaveOrUpdate(customer);

            return customer;
        }

        public void DeleteCustomerByName(string name)
        {
            _customerRepository.DeleteCustomerByName(name);
        }

        public IList<Customer> GetCustomersByName(string name)
        {
            return _customerRepository.GetCustomersByName(name);
        }
    }
}